//
//  CTPPhoneSelectorController.m
//  照片选择器
//
//  Created by 檀志文 on 16/5/19.
//  Copyright © 2016年 ios檀志文. All rights reserved.
//

#import "CTPPhoneSelectorController.h"
#import "CTPPhotoSelectorCell.h"
#import "UIImage+AsyncDraw.h"

@interface CTPPhoneSelectorController ()<CMPPhotoSelectorCellDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
//layout
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;

@property (nonatomic, assign) NSInteger maxCount;

@end

@implementation CTPPhoneSelectorController

static NSString * const reuseIdentifier = @"Cell";


#pragma mark - 实现代理协议的两个方法
// 当选中图片就会调用这个代理方法
// 当实现这个代理协议,需要我们自己来关闭控制器
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary<NSString *,id> *)editingInfo{
    //  等比例的缩小，宽度缩小到300
    CGFloat newWidth = 300;
    CGFloat newHeight = newWidth *image.size.height/image.size.width;
    //将图片缩小
    //自定义一个方法绘制一个图片
    [image hm_AsyncDrawImagesize:CGSizeMake(newWidth, newHeight) bkgColor:[UIColor whiteColor] isCorner:NO drawFinish:^(UIImage *image) {
    // 绘制好的图片
        [self.photos addObject:image];
    // 刷新新数据
        [self.collectionView reloadData];
    // 关闭控制器
        [picker dismissViewControllerAnimated:YES completion:nil];
    }];
}

#pragma mark - 实现代理方法
-(void)photoSelectorCellDidClickAddButton:(CTPPhotoSelectorCell *)cell{
//  系统有一个照片选择器(必须实现两个代理协议，一个都不能少，UIImagePickerControllerDelegate，UINavigationControllerDelegate)
    UIImagePickerController *pickerVc = [[UIImagePickerController alloc]init];
    pickerVc.delegate = self;
    [self presentViewController:pickerVc animated:YES completion:nil];
    
}

-(void)photoSelectorCellDidClickDeleteButton:(CTPPhotoSelectorCell *)cell{
    //获取cell的indexPath
    NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
    //删除图片
    [_photos removeObjectAtIndex:indexPath.item];
    //刷新数据
    [self.collectionView reloadData];
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _photos.count < _maxCount ? _photos.count + 1 : _photos.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CTPPhotoSelectorCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor brownColor];
    cell.delegate= self;
    if (indexPath.item <_photos.count) {
        cell.image = _photos[indexPath.item];
    }else{
//        加号按钮设置
        [cell setupAddButton];
    }
    return cell;
}

# pragma mark - 生命周期的方法
- (void)viewDidLoad {
    [super viewDidLoad];
//
    _maxCount = 6;
    NSMutableArray <UIImage *> *imageArr = [NSMutableArray arrayWithCapacity:_maxCount];
}

#pragma mark - 初始化设置
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        NSLog(@"init(coder:) has not been implemented");
    }
    return self;
}

#warning 重写这个方法就是添加flowlayout参数的时候
-(instancetype)init{
    if (self =[super initWithCollectionViewLayout:self.layout]) {
//        [self setUpFlowLayout];
        self.collectionView.pagingEnabled = YES;
        self.collectionView.bounces = NO;
        self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.layout];
        //    UICollectionViewCell 测试
        self.collectionView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
        [self.collectionView registerClass:[CTPPhotoSelectorCell class] forCellWithReuseIdentifier:reuseIdentifier];
        [self.collectionView setBackgroundColor:[UIColor whiteColor]];
    }
    return self;
}

#pragma mark - 懒加载
- (NSMutableArray<UIImage *> *)photos{
    if (_photos == nil) {

        _photos = [[NSMutableArray alloc]init];
    }
    return _photos;
}

- (UICollectionViewFlowLayout *)layout{
    if (_layout == nil) {
        _layout = [[UICollectionViewFlowLayout alloc] init];
//        设置layout的大小
        _layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        /// 模型提供好用一点的图片NSURL数组
        _layout.itemSize = CGSizeMake(80, 80);
        _layout.minimumInteritemSpacing = 0;
        _layout.minimumLineSpacing = 0;
    }
    return _layout;
}


@end
